package weaver.interfaces.zhlx.duss.action.hrmresourcemodule.hcm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.HcmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 正负激励管理流程调用SAP正负激励接口
 * @ModifyLog:
 **/
public class WeaZfjlgllcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaZfjlgllcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用HCM系统接口返回:";
            RecordSet rs = new RecordSet();
            HcmUtil hcmUtil = new HcmUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String bjlrlx = Util.null2String(rs.getString("bjlrlx"));
            String tbdh = Util.null2String(rs.getString("tbdh"));
            String zt = Util.null2String(rs.getString("zt"));
            if (bjlrlx.contains("0")) {
                JSONArray dataList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject dataJson = new JSONObject();
                    String gh = Util.null2String(rs.getString("gh"));
                    String zxrq = Util.null2String(rs.getString("zxrq"));
                    String fw = Util.null2String(rs.getString("fw"));
                    String jllx = Util.null2String(rs.getString("jllx"));
                    String jlxs = Util.null2String(rs.getString("jlxs"));
                    String jlje = Util.null2String(rs.getString("jlje"));
                    String jllb = Util.null2String(rs.getString("jllb"));
                    String zrrlx = Util.null2String(rs.getString("zrrlx"));
                    String bz = Util.null2String(rs.getString("bz"));
                    dataJson.put("PERNR", gh);
                    dataJson.put("BEGDA", hcmUtil.formatDate(zxrq));
                    dataJson.put("ENDDA", "99991231");
                    dataJson.put("ZZJLFW", fw);
                    dataJson.put("ZZJILX", jllx);
                    dataJson.put("ZZJLXS", jlxs);
                    dataJson.put("ZZJLJE", jlje);
                    dataJson.put("ZZJLXM", jllb);
                    dataJson.put("ZZRRLX", zrrlx);
                    dataJson.put("ZZRRGH", gh);
                    dataJson.put("ZZTBBH", tbdh);
                    dataJson.put("ZZTBZT", zt);
                    dataJson.put("ZZBZ", bz);
                    dataList.add(dataJson);
                }
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaZfjlgllcAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaZfjlgllcAction>>>>>dataList=" + dataList);
                JSONObject resultJson = hcmUtil.handleOKHttpRequest("EC203", dataList, dataMap);
                writeLog(">>>>>WeaZfjlgllcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if (!"S".equals(msgType)) {
                        message += outputJson.getString("MSG_TEXT");
                        String returnData = outputJson.getString("DATA");
                        JSONArray returnDataList = JSONArray.parseArray(returnData);
                        for (int i = 0; i < returnDataList.size(); i++) {
                            String msgty = returnDataList.getJSONObject(i).getString("MSGTY");
                            if("E".equalsIgnoreCase(msgty)){
                                message += returnDataList.getJSONObject(i).getString("PERNR");
                                message += returnDataList.getJSONObject(i).getString("MSGTX");
                            }
                        }
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZfjlgllcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}